const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
  if (event.limit && event.limit > 0) {
    return await db.collection(event.db_name).aggregate()
      .lookup(event.lookup)
      .match(event.match)
      .sort(event.sort)
      .skip(event.skip)
      .limit(event.limit)
      .end()
  } else {
    return await db.collection(event.db_name).aggregate()
      .lookup(event.lookup)
      .match(event.match)
      .sort(event.sort)
      .end()
  }
}